package impl.defaultImpl;

import design.Input;
import design.Switcher;
import design.entity.Standard;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @ClassName DefaultDocxSwitcher .java
 * @Description empty
 * @Author LiuJinyu
 * @Date 2023/4/2 20:06
 * @Version 1.0
 */

public class DefaultDocxSwitcher extends Switcher {
    private static final Logger log = LogManager.getLogger(DefaultDocxFileInput.class);
    private LocalDate nextWeek;
    /**
     * df1是HH:mm,df2是H:mm
     */
    private DateTimeFormatter df1,df2;
    //todo 周六
    //todo debug
    //todo doc

    public DefaultDocxSwitcher() {
        super();
        df1 = DateTimeFormatter.ofPattern("HH:mm");
        df2 = DateTimeFormatter.ofPattern("H:mm");
        changeThisLocalDateToNextWeek();
    }

    @Override
    public List<Standard> conversion(String origin) {
        //去除\t\t、\n、空格
        origin = origin.replaceAll("\t\t", "\t");
        origin = origin.replaceAll("\n", "");
        origin = origin.replaceAll(" ","");
        origin = origin.replaceAll("：",":");
        String[] deep1 = origin.split("星期[一二三四五六日天]");

        if(deep1.length<=1){
            deep1 = origin.split("周[一二三四五六日天]");
        }
        if(log.isDebugEnabled()){
            log.debug(origin.replaceAll("\t","t"));
        }
        for (int i = 1; i < deep1.length; i++) {
            deep2ToStandard(deep1[i]);
            nextWeek = nextWeek.plusDays(1);
        }
        return this.list;
    }

    private void deep2ToStandard(String deep1Str) {
        String[] deep2 = deep1Str.split("\t");
        Standard standard = null;
        for (int i = 1; i < deep2.length-1; i+=2) {
            standard = new Standard();
            standard.setDate(nextWeek);
            standard.setContent(deep2[i]);
            int timeSeparator = deep2[i+1].indexOf("-");
            String b = deep2[i+1].substring(0,timeSeparator);
            String e = deep2[i+1].substring(timeSeparator+1);
            standard.setBeginTime(b.length()<5?LocalTime.parse(b,df2):LocalTime.parse(b,df1));
            standard.setEndTime(e.length()<5?LocalTime.parse(e,df2):LocalTime.parse(e,df1));
            standard.setAlert("DISPLAY");
            list.add(standard);
        }
    }

    private void changeThisLocalDateToNextWeek() {
        LocalDate now = LocalDate.now();
        int nowInt = now.getDayOfWeek().getValue();
        if (nowInt >= 5) {
            this.nextWeek = now.plusDays(8-nowInt);
        }else{
            this.nextWeek = now.minusDays(nowInt-1);
        }
    }
}
